﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections;
using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public class event_management
    {

        dbDataContext db_remote = new dbDataContext();
        Local_db db_local = new Local_db("Data Source=local_db_remote.sdf;Max Database Size=2047");

        /****************************************************/
        /*         fonction give_me_all_from_date           */   
        /* entrée : date de type DateTime                   */
        /* sortie : vecteur personList : une liste contenant*/
        /*        les objets new_event1                     */
        /****************************************************/
        public ArrayList give_me_all_from_date(DateTime date)
        {      
            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/"+ date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (events s in (from e in db_remote.events where e.T_begin >= date_begin && e.T_begin <= date_end select e))
            {
                name_person = give_me_name((int)s.id_person, s.type_person);

                string period_name = (from i in db_remote.periods
                                      where i.id == s.period_id
                                      select i.name_period).FirstOrDefault();

                string room_name = (from i in db_remote.rooms
                                      where i.id == s.room_id
                                      select i.name).FirstOrDefault();
            
                new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                personList.Add(new_event1);
            }
            
            return personList;
        }


        public ArrayList give_me_all_from_date_offline(DateTime date)
        {
            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (Events s in (from e in db_local.Events where e.T_begin >= date_begin && e.T_begin <= date_end select e))
            {
                name_person = give_me_name_offline((int)s.Id_person, s.Type_person);

                string period_name = (from i in db_local.Period
                                      where i.Id == s.Period_id
                                      select i.Name_period).FirstOrDefault();

                string room_name = (from i in db_local.Rooms
                                    where i.Id == s.Room_id
                                    select i.Name).FirstOrDefault();

                new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                personList.Add(new_event1);
            }

            return personList;
        }

        /****************************************************/
        /*give_me_id_person_from_participant_and_type_person*/
        /* entrée : string: type de person et nom de person */
        /* sortie : int : id de la personne                 */
        /*                                                  */
        /****************************************************/
        public int give_me_id_person_from_participant_and_type_person(string type,string name)
        {
           
            int id = 0;
            switch (type)
            {
                case "administrators":
                    foreach (administrators s in (from e in db_remote.administrators where e.login == name select e))
                    { return s.id; }
                    break;
                case "managers":
                    foreach (managers s in (from e in db_remote.managers where e.login == name select e))
                    { return s.id; }
                    break;
                case "students":
                    foreach (students s in (from e in db_remote.students where e.login == name select e))
                    { return s.id; }
                    break;
                case "teacher":
                    foreach (teacher s in (from e in db_remote.teacher where e.login == name select e))
                    { return s.id; }
                    break;
                default:
                    return id;
            }
            return id;
        }

        public int give_me_id_person_from_participant_and_type_person_offline(string type, string name)
        {
            int id = 0;
            switch (type)
            {
                case "administrators":
                    foreach (Administrators s in (from e in db_local.Administrators where e.Login == name select e))
                    { return s.Id; }
                    break;
                case "managers":
                    foreach (Managers s in (from e in db_local.Managers where e.Login == name select e))
                    { return s.Id; }
                    break;
                case "students":
                    foreach (Students s in (from e in db_local.Students where e.Login == name select e))
                    { return s.Id; }
                    break;
                case "teacher":
                    foreach (Teacher s in (from e in db_local.Teacher where e.Login == name select e))
                    { return s.Id; }
                    break;
                default:
                    return id;
            }
            return id;
        }


        //Method to find the name with the id and the type_person
        public string give_me_name(int id, string type_person) 
        {
            string name = "";
 
            switch (type_person)
            {
                case "administrators":
                    foreach (administrators s1 in (from e in db_remote.administrators where e.id == id select e))
                    { name = s1.login; }
                    break;
                case "managers":
                    foreach (managers s1 in (from e in db_remote.managers where e.id == id select e))
                    { name = s1.login; }
                    break;
                case "students":
                    foreach (students s1 in (from e in db_remote.students where e.id == id select e))
                    { name = s1.login; }
                    break;
                case "teacher":
                    foreach (teacher s1 in (from e in db_remote.teacher where e.id == id select e))
                    { name = s1.login; }
                    break;
                default:
                    name = "default";
                    break;
            }

            return name;
        }


        public string give_me_name_offline(int id, string type_person)
        {
            string name = "";

            switch (type_person)
            {
                case "administrators":
                    foreach (Administrators s1 in (from e in db_local.Administrators where e.Id == id select e))
                    { name = s1.Login; }
                    break;
                case "managers":
                    foreach (Managers s1 in (from e in db_local.Managers where e.Id == id select e))
                    { name = s1.Login; }
                    break;
                case "students":
                    foreach (Students s1 in (from e in db_local.Students where e.Id == id select e))
                    { name = s1.Login; }
                    break;
                case "teacher":
                    foreach (Teacher s1 in (from e in db_local.Teacher where e.Id == id select e))
                    { name = s1.Login; }
                    break;
                default:
                    name = "default";
                    break;
            }

            return name;
        }
       
        //Method to get all the university events (without personal)
        public ArrayList give_all_university_event(DateTime date)
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();

            if (token.Database_mode == "online")
            {
                DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
                DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
                string name_person = "";
                ArrayList personList = new ArrayList();
                Event new_event1;
                foreach (events s in (from e in db_remote.events
                                      where e.type != "Personal" && e.T_begin >= date_begin && e.T_begin <= date_end
                                      select e))
                {
                    name_person = give_me_name((int)s.id_person, s.type_person);

                    string period_name = (from i in db_remote.periods
                                          where i.id == s.period_id
                                          select i.name_period).FirstOrDefault();

                    string room_name = (from i in db_remote.rooms
                                        where i.id == s.room_id
                                        select i.name).FirstOrDefault();

                    new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                    personList.Add(new_event1);
                }

                return personList;
            }
            else
            {
                return give_all_university_event_offline(date);
            }
        }

        public ArrayList give_all_university_event_offline(DateTime date)
        {
            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (Events s in (from e in db_local.Events
                                  where e.Type != "Personal" && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {
                name_person = give_me_name_offline((int)s.Id_person, s.Type_person);

                string period_name = (from i in db_local.Period
                                      where i.Id == s.Period_id
                                      select i.Name_period).FirstOrDefault();

                string room_name = (from i in db_local.Rooms
                                    where i.Id == s.Room_id
                                    select i.Name).FirstOrDefault();

                new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                personList.Add(new_event1);
            }

            return personList;
        }
        
        //Method to get all the campus event (without perso)
        public ArrayList give_all_campus_event(DateTime date, int campus_id)
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();

            if (token.Database_mode == "online")
            {
                string campus_name = (from i in db_remote.campus
                                      where i.id == campus_id
                                      select i.name).FirstOrDefault();

                DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
                DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
                string name_person = "";
                ArrayList personList = new ArrayList();
                Event new_event1;
                foreach (events s in (from e in db_remote.events
                                      where e.type != "Personal" && e.campus == campus_name && e.T_begin >= date_begin && e.T_begin <= date_end
                                      select e))
                {
                    name_person = give_me_name((int)s.id_person, s.type_person);

                    string period_name = (from i in db_remote.periods
                                          where i.id == s.period_id
                                          select i.name_period).FirstOrDefault();

                    string room_name = (from i in db_remote.rooms
                                        where i.id == s.room_id
                                        select i.name).FirstOrDefault();

                    new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                    personList.Add(new_event1);
                }

                return personList;
            }
            else
            {
                return give_all_campus_event_offline(date, campus_id);
            }
        }

        public ArrayList give_all_campus_event_offline(DateTime date, int campus_id)
        {

            string campus_name = (from i in db_local.Campus
                                  where i.Id == campus_id
                                  select i.Name).FirstOrDefault();

            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (Events s in (from e in db_local.Events
                                  where e.Type != "Personal" && e.Campus == campus_name && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {
                name_person = give_me_name_offline((int)s.Id_person, s.Type_person);

                string period_name = (from i in db_local.Period
                                      where i.Id == s.Period_id
                                      select i.Name_period).FirstOrDefault();

                string room_name = (from i in db_local.Rooms
                                    where i.Id == s.Room_id
                                    select i.Name).FirstOrDefault();

                new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                personList.Add(new_event1);
            }

            return personList;
        }

        //Method to get all the class events (without perso)
        public ArrayList give_all_class_event(DateTime date, string campus_name, string class_name)
        {
            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (events s in (from e in db_remote.events
                                  where e.type != "Personal" && e.campus == campus_name && e.classe == class_name && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {
                name_person = give_me_name((int)s.id_person, s.type_person);

                string period_name = (from i in db_remote.periods
                                      where i.id == s.period_id
                                      select i.name_period).FirstOrDefault();

                string room_name = (from i in db_remote.rooms
                                    where i.id == s.room_id
                                    select i.name).FirstOrDefault();

                new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                personList.Add(new_event1);
            }

            return personList;
        }


        public ArrayList give_all_class_event_offline(DateTime date, string campus_name, string class_name)
        {

            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (Events s in (from e in db_local.Events
                                  where e.Type != "Personal" && e.Campus == campus_name && e.Classe == class_name && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {
                name_person = give_me_name_offline((int)s.Id_person, s.Type_person);

                string period_name = (from i in db_local.Period
                                      where i.Id == s.Period_id
                                      select i.Name_period).FirstOrDefault();

                string room_name = (from i in db_local.Rooms
                                    where i.Id == s.Room_id
                                    select i.Name).FirstOrDefault();

                new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                personList.Add(new_event1);
            }

            return personList;
        }


        //Method to get the perso calendar of a the connected user
        public ArrayList give_all_perso_event(DateTime date, int id_person, string type_person)
        {

            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (events s in (from e in db_remote.events
                                  where e.type == "Personal" && e.id_person == id_person && e.type_person == type_person && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {
                name_person = give_me_name((int)s.id_person, s.type_person);

                string period_name = (from i in db_remote.periods
                                      where i.id == s.period_id
                                      select i.name_period).FirstOrDefault();

                string room_name = (from i in db_remote.rooms
                                    where i.id == s.room_id
                                    select i.name).FirstOrDefault();

                new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                personList.Add(new_event1);
            }

            return personList;
        }

        public ArrayList give_all_perso_event_offline(DateTime date, int id_person, string type_person)
        {

            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (Events s in (from e in db_local.Events
                                  where e.Type == "Personal" && e.Id_person == id_person && e.Type_person == type_person && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {
                name_person = give_me_name_offline((int)s.Id_person, s.Type_person);

                string period_name = (from i in db_local.Period
                                      where i.Id == s.Period_id
                                      select i.Name_period).FirstOrDefault();

                string room_name = (from i in db_local.Rooms
                                    where i.Id == s.Room_id
                                    select i.Name).FirstOrDefault();

                new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                personList.Add(new_event1);
            }

            return personList;
        }


     
        
        //Method to get all the cursus event 
        public ArrayList give_all_cursus_event(DateTime date, int cursus_id)
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();

            if (token.Database_mode == "online")
            {
                DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
                DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
                string name_person = "";
                ArrayList personList = new ArrayList();
                Event new_event1;


                //Selecte all the periods of a cursus
                foreach (period p in (from f in db_remote.periods
                                      where f.cursus_id == cursus_id
                                      select f))
                {

                    foreach (events s in (from e in db_remote.events
                                          where e.type != "Personal" && e.period_id == p.id && e.T_begin >= date_begin && e.T_begin <= date_end
                                          select e))
                    {

                        name_person = give_me_name((int)s.id_person, s.type_person);

                        string period_name = (from i in db_remote.periods
                                              where i.id == s.period_id
                                              select i.name_period).FirstOrDefault();

                        string room_name = (from i in db_remote.rooms
                                            where i.id == s.room_id
                                            select i.name).FirstOrDefault();

                        new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                        personList.Add(new_event1);
                    }

                }

                return personList;
            }
            else
            {
                return give_all_cursus_event_offline(date, cursus_id);
            }
        }


        public ArrayList give_all_cursus_event_offline(DateTime date, int cursus_id)
        {
           
                DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
                DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
                string name_person = "";
                ArrayList personList = new ArrayList();
                Event new_event1;


                //Selecte all the periods of a cursus
                foreach (Period p in (from f in db_local.Period
                                      where f.Cursus_id == cursus_id
                                      select f))
                {
                    
                    foreach (Events s in (from e in db_local.Events
                                          where e.Type != "Personal" && e.Period_id == p.Id && e.T_begin >= date_begin && e.T_begin <= date_end
                                          select e))
                    {

                        name_person = give_me_name((int)s.Id_person, s.Type_person);

                        string period_name = (from i in db_local.Period
                                              where i.Id == s.Period_id
                                              select i.Name_period).FirstOrDefault();

                        string room_name = (from i in db_local.Rooms
                                            where i.Id == s.Room_id
                                            select i.Name).FirstOrDefault();

                        new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                        personList.Add(new_event1);
                    }

                }

                return personList;
           
        }


        public ArrayList give_all_class_event(DateTime date, int class_id)
        {
             SingletonTokenUser token = SingletonTokenUser.GetInstance();

             if (token.Database_mode == "online")
             {

                 string class_name = (from i in db_remote.classes
                                      where i.id == class_id
                                      select i.name).FirstOrDefault();

                 int campus_id = (from i in db_remote.classes
                                  where i.id == class_id
                                  select i.id).FirstOrDefault();

                 string campus_name = (from i in db_remote.campus
                                       where i.id == campus_id
                                       select i.name).FirstOrDefault();

                 DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
                 DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
                 string name_person = "";
                 ArrayList personList = new ArrayList();
                 Event new_event1;
                 foreach (events s in (from e in db_remote.events
                                       where e.type != "Personal" && e.campus == campus_name && e.classe == class_name && e.T_begin >= date_begin && e.T_begin <= date_end
                                       select e))
                 {
                     name_person = give_me_name((int)s.id_person, s.type_person);

                     string period_name = (from i in db_remote.periods
                                           where i.id == s.period_id
                                           select i.name_period).FirstOrDefault();

                     string room_name = (from i in db_remote.rooms
                                         where i.id == s.room_id
                                         select i.name).FirstOrDefault();

                     new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                     personList.Add(new_event1);
                 }

                 return personList;
             }
             else
             {
                 return give_all_class_event_offline(date, class_id);
             }
        }

        public ArrayList give_all_class_event_offline(DateTime date, int class_id)
        {

            string class_name = (from i in db_local.Classes
                                 where i.Id == class_id
                                 select i.Name).FirstOrDefault();

            int campus_id = Convert.ToInt32((from z in db_local.Classes
                             where z.Id == class_id
                             select z.Campus_id).FirstOrDefault());

            string campus_name = (from i in db_local.Campus
                                  where i.Id == campus_id
                                  select i.Name).FirstOrDefault();

            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;
            foreach (Events s in (from e in db_local.Events
                                  where e.Type != "Personal" && e.Campus == campus_name && e.Classe == class_name && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {
                name_person = give_me_name((int)s.Id_person, s.Type_person);

                string period_name = (from i in db_local.Period
                                      where i.Id == s.Period_id
                                      select i.Name_period).FirstOrDefault();

                string room_name = (from i in db_local.Rooms
                                    where i.Id == s.Room_id
                                    select i.Name).FirstOrDefault();

                new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                personList.Add(new_event1);
            }

            return personList;
        }


        public ArrayList give_all_period_event(DateTime date, int period_id, string campus_name)
        {
            SingletonTokenUser token = SingletonTokenUser.GetInstance();

            if (token.Database_mode == "online")
            {
                DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
                DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
                string name_person = "";
                ArrayList personList = new ArrayList();
                Event new_event1;


                foreach (events s in (from e in db_remote.events
                                      where e.type != "Personal" && e.period_id == period_id && e.campus == campus_name && e.T_begin >= date_begin && e.T_begin <= date_end
                                      select e))
                {

                    name_person = give_me_name((int)s.id_person, s.type_person);

                    string period_name = (from i in db_remote.periods
                                          where i.id == s.period_id
                                          select i.name_period).FirstOrDefault();

                    string room_name = (from i in db_remote.rooms
                                        where i.id == s.room_id
                                        select i.name).FirstOrDefault();

                    new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                    personList.Add(new_event1);
                }


                return personList;
            }

            else
            {
                return give_all_period_event_offline(date, period_id, campus_name);
            }
        }

        public ArrayList give_all_period_event_offline(DateTime date, int period_id, string campus_name)
        {
            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;


            foreach (Events s in (from e in db_local.Events
                                  where e.Type != "Personal" && e.Period_id == period_id && e.Campus == campus_name && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {

                name_person = give_me_name((int)s.Id_person, s.Type_person);

                string period_name = (from i in db_local.Period
                                      where i.Id == s.Period_id
                                      select i.Name_period).FirstOrDefault();

                string room_name = (from i in db_local.Rooms
                                    where i.Id == s.Room_id
                                    select i.Name).FirstOrDefault();

                new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                personList.Add(new_event1);
            }


            return personList;
        }

        public ArrayList give_all_user_event(DateTime date, int user_id, string user_type)
        {
             SingletonTokenUser token = SingletonTokenUser.GetInstance();

             if (token.Database_mode == "online")
             {
                 DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
                 DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
                 string name_person = "";
                 ArrayList personList = new ArrayList();
                 Event new_event1;


                 foreach (events s in (from e in db_remote.events
                                       where e.type_person == user_type && e.id_person == user_id && e.T_begin >= date_begin && e.T_begin <= date_end
                                       select e))
                 {

                     name_person = give_me_name((int)s.id_person, s.type_person);

                     string period_name = (from i in db_remote.periods
                                           where i.id == s.period_id
                                           select i.name_period).FirstOrDefault();

                     string room_name = (from i in db_remote.rooms
                                         where i.id == s.room_id
                                         select i.name).FirstOrDefault();

                     new_event1 = new Event(s.id, s.name, s.priority, s.type, s.T_begin, s.T_end, s.classe, name_person, s.campus, period_name, room_name);
                     personList.Add(new_event1);
                 }


                 return personList;
             }
             else
             {
                 return give_all_user_event_offline(date, user_id, user_type);
             }
        }

        public ArrayList give_all_user_event_offline(DateTime date, int user_id, string user_type)
        {
            DateTime date_begin = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 07:00:00");
            DateTime date_end = DateTime.Parse(date.Year + "/" + date.Month + "/" + date.Day + " 22:00:00");
            string name_person = "";
            ArrayList personList = new ArrayList();
            Event new_event1;


            foreach (Events s in (from e in db_local.Events
                                  where e.Type_person == user_type && e.Id_person == user_id && e.T_begin >= date_begin && e.T_begin <= date_end
                                  select e))
            {

                name_person = give_me_name((int)s.Id_person, s.Type_person);

                string period_name = (from i in db_local.Period
                                      where i.Id == s.Period_id
                                      select i.Name_period).FirstOrDefault();

                string room_name = (from i in db_local.Rooms
                                    where i.Id == s.Room_id
                                    select i.Name).FirstOrDefault();

                new_event1 = new Event(s.Id, s.Name, s.Priority, s.Type, s.T_begin, s.T_end, s.Classe, name_person, s.Campus, period_name, room_name);
                personList.Add(new_event1);
            }


            return personList;
        }

    }
}



